package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.school.entity.School;

public class SchoolRowMapper implements RowMapper<School> {

	private int type;

	public static final int AND_MANAGERS = 1;

	public static final int AND_USERNUMS = 2;

	public static final int AND_PARENTNUMS = 3;

	public static final int AND_MESSAGE = 4;

	public static final int AND_BUY = 5;

	public SchoolRowMapper() {
	}

	public SchoolRowMapper(int type) {
		this.type = type;
	}

	@Override
	public School mapRow(ResultSet rs, int rowNum) throws SQLException {
		School s = parseSchool(new School(), rs);
		switch (type) {
		case AND_MANAGERS:
			s.setManagers(rs.getString("managers"));// 管理员名字账号
			break;
		case AND_USERNUMS:
			s.setNum_teacher(rs.getInt("num_teacher"));// 老师人数
			s.setNum_student(rs.getInt("num_student"));// 学生人数
			s.setNum_parent(rs.getInt("num_parent"));// 家长人事
			break;
		case AND_PARENTNUMS:
			s.setNum_parent(rs.getInt("num_parent"));// 家长人事
			break;
		case AND_MESSAGE:
			s.setCount(rs.getInt("count"));// 短信条数
			break;
		case AND_BUY:
			s.setNum_parent(rs.getInt("num_parent"));
			if (rs.getInt("method") == 1 || rs.getInt("method") == 0) {
				s.setMethodStr("月付费用户");
			} else {
				s.setMethodStr("购买短信用户");
			}
			break;
		}
		return s;
	}

	public static School parseSchool(School s, ResultSet rs)
			throws SQLException {
		return parseSchool(s, rs, null);
	}

	public static School parseSchool(School s, ResultSet rs, String su)
			throws SQLException {

		if (StringUtil.isStringNull(su)) {
			su = "";
		} else {
			su += ".";
		}
		try {
			s.setSid(rs.getInt(su + "sid"));// sid
		} catch (SQLException e) {
			s.setSid(0);
		}

		try {
			s.setName(rs.getString(su + "name"));// name
		} catch (SQLException e) {
			s.setName("");
		}

		try {
			s.setPid(rs.getInt(su + "pid"));// pid
		} catch (SQLException e) {
			s.setPid(0);
		}

		try {
			s.setCid(rs.getInt(su + "cid"));// cid
		} catch (SQLException e) {
			s.setCid(0);
		}

		try {
			s.setDesinfo(rs.getString(su + "desinfo"));// desinfo
		} catch (SQLException e) {
			s.setDesinfo("");
		}

		try {
			s.setImg(rs.getString(su + "img"));// img
		} catch (SQLException e) {
			s.setImg("");
		}

		try {
			s.setSlogo(rs.getString(su + "slogo"));// slogo
		} catch (SQLException e) {
			s.setSlogo("");
		}

		try {
			s.setMesCount(rs.getInt(su + "mesCount"));// mesCount
		} catch (SQLException e) {
			s.setMesCount(0);
		}

		try {
			s.setMethod(rs.getInt(su + "method"));// method
		} catch (SQLException e) {
			s.setMethod(0);
		}

		try {
			s.setClassCount(rs.getInt(su + "classCount"));// classCount
		} catch (SQLException e) {
			s.setClassCount(0);
		}

		try {
			s.setMesSuffix(rs.getString(su + "mesSuffix"));// mesSuffix
		} catch (SQLException e) {
			s.setMesSuffix("");
		}

		try {
			s.setManagers(rs.getString("managers"));// managers
		} catch (SQLException e) {
			s.setManagers("");
		}

		try {
			s.setNum_teacher(rs.getInt("num_teacher")); // num_teacher
		} catch (SQLException e) {
			s.setNum_teacher(0);
		}

		try {
			s.setNum_student(rs.getInt("num_student")); // num_student
		} catch (SQLException e) {
			s.setNum_student(0);
		}

		try {
			s.setNum_parent(rs.getInt("num_parent"));// num_parent
		} catch (SQLException e) {
			s.setNum_parent(0);
		}

		try {
			s.setCount(rs.getInt("count"));// count
		} catch (SQLException e) {
			s.setCount(0);
		}

		try {
			s.setCount(rs.getInt("memberNum"));// count
		} catch (SQLException e) {
			s.setCount(0);
		}

		return s;
	}

	public static Map<String, Object> getPar(Map<String, Object> par, School s) {
		par.put("sid", s.getSid());
		par.put("name", s.getName());
		par.put("cid", s.getCid());
		par.put("pid", s.getPid());
		par.put("desinfo", s.getDesinfo());
		par.put("img", s.getImg());
		par.put("slogo", s.getSlogo());
		par.put("mesCount", s.getMesCount());
		par.put("method", s.getMethod());
		par.put("classCount", s.getClassCount());
		par.put("mesSuffix", s.getMesSuffix());
		par.put("managers", s.getManagers());
		par.put("num_teacher", s.getNum_teacher());
		par.put("num_student", s.getNum_student());
		par.put("num_parent", s.getNum_parent());
		par.put("count", s.getCount());
		par.put("memberNum", s.getMemberNum());
		return par;
	}

	public static Map<String, Object> getArgs(Map<String, Object> args, School s) {
		args.put("sid", s.getSid());
		return args;
	}

	/**
	 * 
	 * 
	 * @param tfp
	 * @return
	 */
	public static PreparedStatementCreator getPreparedStatementCreator(
			final School s) {
		return new PreparedStatementCreator() {
			@Override
			public PreparedStatement createPreparedStatement(Connection con)
					throws SQLException {
				PreparedStatement ps = con.prepareStatement(parseSQL(s),
						new String[] { "sid" });
				parsePS(ps, s);
				return ps;
			}

		};
	}

	private static String parseSQL(School s) throws SQLException {
		StringBuffer sql = new StringBuffer("insert into school(");
		int count = 0;
		if (null != s.getName()) {
			sql.append("name,");
			count++;
		}
		if (null != s.getPid()) {
			sql.append("pid,");
			count++;
		}
		if (null != s.getCid()) {
			sql.append("cid,");
			count++;
		}
		if (null != s.getDesinfo()) {
			sql.append("desinfo,");
			count++;
		}
		if (null != s.getImg()) {
			sql.append("img,");
			count++;
		}
		if (null != s.getSlogo()) {
			sql.append("slogo,");
			count++;
		}
		if (null != s.getMesCount()) {
			sql.append("mesCount,");
			count++;
		}
		if (null != s.getMethod()) {
			sql.append("method,");
			count++;
		}
		if (null != s.getMesSuffix()) {
			sql.append("mesSuffix,");
			count++;
		}

		if (null != s.getClassCount()) {
			sql.append("classCount,");
			count++;
		}
		if (null != s.getManagers()) {
			sql.append("managers,");
			count++;
		}
		if (null != s.getNum_teacher()) {
			sql.append("num_teacher,");
			count++;
		}

		if (null != s.getNum_student()) {
			sql.append("num_parent,");
			count++;
		}

		if (null != s.getNum_parent()) {
			sql.append("num_student,");
			count++;
		}

		if (null != s.getCount()) {
			sql.append("count,");
			count++;
		}

		if (null != s.getMemberNum()) {
			sql.append("memberNum,");
			count++;
		}

		sql = new StringBuffer(sql.substring(0, sql.length() - 1));
		sql.append(") value(");
		for (int i = 0; i < (count - 1); i++) {
			sql.append("?,");
		}
		return sql.append("?)").toString();
	}

	private static void parsePS(PreparedStatement ps, School s)
			throws SQLException {
		int count = 1;
		if (null != s.getName()) {
			ps.setString(count++, s.getName());
		}
		if (null != s.getPid()) {
			ps.setInt(count++, s.getPid());
		}
		if (null != s.getCid()) {
			ps.setInt(count++, s.getCid());
		}
		if (null != s.getDesinfo()) {
			ps.setString(count++, s.getDesinfo());
		}
		if (null != s.getImg()) {
			ps.setString(count++, s.getImg());
		}
		if (null != s.getSlogo()) {
			ps.setString(count++, s.getSlogo());
		}
		if (null != s.getMesCount()) {
			ps.setInt(count++, s.getMesCount());
		}
		if (null != s.getMethod()) {
			ps.setInt(count++, s.getMethod());
		}
		if (null != s.getMesSuffix()) {
			ps.setString(count++, s.getMesSuffix());
		}

		if (null != s.getClassCount()) {
			ps.setInt(count++, s.getClassCount());

		}
		if (null != s.getManagers()) {
			ps.setString(count++, s.getManagers());

		}
		if (null != s.getNum_teacher()) {
			ps.setInt(count++, s.getNum_teacher());
		}

		if (null != s.getNum_student()) {
			ps.setInt(count++, s.getNum_student());
		}

		if (null != s.getNum_parent()) {
			ps.setInt(count++, s.getNum_parent());
		}

		if (null != s.getCount()) {
			ps.setInt(count++, s.getCount());
		}

		if (null != s.getMemberNum()) {
			ps.setInt(count++, s.getMemberNum());
		}
	}
}
